import 'dart:ffi';

import 'package:flutter/material.dart';

class ChipPage extends StatelessWidget {
  const ChipPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('chip demo'),
      ),
      body: Center(
        child: Column(
          children: [
            ActionChip(
              label: const Text('Action chip'),
              onPressed: () {
                print('If you stand for nothing, Burr, what\'ll you fall for?');
              },
              avatar: CircleAvatar(
                backgroundColor: Colors.grey.shade800,
                child: const Text('AB'),
              ),
            ),
            RawChip(
              avatar: const CircleAvatar(
                child: Text('R'),
              ),
              label: const Text('RawChip'),
              onDeleted: () {
                print('delete action');
              },
              deleteIcon: const Icon(Icons.delete),
              deleteIconColor: Colors.red,
              deleteButtonTooltipMessage: '删除',
            ),
            ChipContent(),
            ChoiceChipDemo(),
            FliterChipDemo(),
          ],
        ),
      ),
    );
  }
}
class ChipContent extends StatefulWidget {
  ChipContent({Key? key}) : super(key: key);
  @override
  _ChipContentState createState() => _ChipContentState();
}
class _ChipContentState extends State<ChipContent> {
  bool _selected = false;
  @override
  Widget build(BuildContext context) {
    return RawChip(
      label: const Text('RawChip'),
      selected: _selected,
      onSelected: (v){
        setState(() {
          _selected = v;
        });
      },
      selectedColor: Colors.blue,
      selectedShadowColor: Colors.red,
    );
  }
}
// 
class ChoiceChipDemo extends StatefulWidget {
  ChoiceChipDemo({Key? key}) : super(key: key);
  @override
  _ChoiceChipDemoState createState() => _ChoiceChipDemoState();
}
class _ChoiceChipDemoState extends State<ChoiceChipDemo> {
  int _selectIndex = 0;
  List<String> temp = ['cho', 'choi', 'choice', 'choice ch', 'choice chip'];
  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 15,
      children: List.generate(10, (index) => ChoiceChip(
        label: Text('${temp[index % 5]} $index'), 
        selected: _selectIndex == index,
        onSelected: (v) {
          setState(() {
            _selectIndex = index;
          });
        },
      )).toList(),
    );
  }
}
//
class FliterChipDemo extends StatefulWidget {
  FliterChipDemo({Key? key}) : super(key: key);
  @override
  _FliterChipDemoState createState() => _FliterChipDemoState();
}

class _FliterChipDemoState extends State<FliterChipDemo> {
  List<String> _filters = [];
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Wrap(
          spacing: 15,
          children: List.generate(10, (index) => FilterChip(
            label: Text('chip $index'), 
            selected: _filters.contains('$index'),
            onSelected: (v) {
              setState(() {
                if (v) {
                  _filters.add('$index');
                } else {
                  _filters.removeWhere((f) => f == '$index');
                }
              });
            },
          )).toList(),
        ),
        Text('选中：${_filters.join(',')}'),
      ],
    );
  }
}